﻿//准备ExtJS环境
Ext.require
(
    [
        'Ext.form.field.File',
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.state.*',
        'Ext.form.*'
    ]
);

//定义用户表格控件
Ext.define
(
    'MaintainMachine.Grid',
    {
        extend: 'Ext.grid.Panel',

        //初始化组件
        initComponent: function ()
        {
            Ext.apply
            (
                this,
                {
                    listeners:
                    {
                        //设置两个指头的滚动
                        render: Common.ScrollableRender
                    },
                    dockedItems:
                    [
                        {
                            xtype: 'toolbar',
                            height: 36,
                            items:
                            [
                                {
                                    iconCls: 'icon-refresh',
                                    text: '刷新',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '刷新数据',
                                    handler: this.onRefreshClick
                                }
                            ]
                        }
                    ]
                }
            );
            this.callParent();
            this.getSelectionModel().on('selectionchange', this.onSelectionChange, this);
        },

        //处理选中养护变化事件
        onSelectionChange: function (selectionModel, selections)
        {
        },

        //处理刷新按钮点击事件
        onRefreshClick: function ()
        {
            //刷新数据
            this.store.load();
        }
    }
);

//挂接环境准备完毕事件
Ext.onReady
(
    function ()
    {
        //初始化快速提示
        Ext.QuickTips.init();

        //设置状态保存提供者（当前设置为Cookie）
        Ext.state.Manager.setProvider( Ext.create( 'Ext.state.CookieProvider', { path: _Current_ActionUrl } ) );

        //初始化附加验证类型
        Common.InitAdditionalValidation();

        //创建数据存储对象
        var store = Ext.create
        (
            'Ext.data.Store',
            {
                pageSize: 20,
                autoSync: true,
                autoLoad: false,
                root: 'data',
                remoteFilter: true,
                remoteSort: true,
                model: 'MaintainMachineModel',
                proxy:
                {
                    type: 'ajax',
                    batchActions: false,
                    url: Common.MakeAbsoluteUrl('Select'),
                    reader:
                    {
                        type: 'json',
                        root: 'data'
                    },
                    listeners:
                    {
                        exception: function (proxy, response, operation, options)
                        {
                            //处理错误
                            Common.HandleProxyError(proxy, response, operation, options);
                        }
                    }
                }
            }
        );

        //过滤数据
        store.filter
        (
            filter_condition
        );

        Ext.create
        (
            'MaintainMachine.Grid',
            {
                store: store,
                stateful: true,
                stateId: 'maintainmachine-view-state-grid',
                height: 600,
                width: '100%',
                id: 'maintainmachine-grid',
                title: '',
                renderTo: 'data-grid',
                columnLines: true,
                viewConfig:
                {
                    stripeRows: true,
                    loadingText: '数据加载中...',
                    singleSelect: true
                },
                //创建分页工具栏
                bbar: Ext.create
                (
                    'Ext.PagingToolbar',
                    {
                        store: store,
                        displayInfo: true,
                        displayMsg: '第 {0} - 第 {1} 条记录，共 {2} 条记录',
                        emptyMsg: ''
                    }
                ),
                columns:
                [
                    {
                        text: '单位名称',
                        width: 160,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Organization',
                        hideable: false
                    },
                    {
                        text: '养护编号',
                        width: 160,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'MaintainCode',
                        hideable: false
                    },
                    {
                        text: '状态',
                        width: 60,
                        sortable: true,
                        align: 'center',
                        dataIndex: 'State',
                        renderer: Common.RenderMaintainDataState
                    },
                    {
                        text: '机械类型',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Machine'
                    },
                    {
                        text: '机械属性',
                        width: 80,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'MachineType'
                    },
                    {
                        text: '台班数量',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer('0,000.00'),
                        dataIndex: 'Quantity'
                    },
                    {
                        text: '台班单价',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer('0,000.000'),
                        dataIndex: 'Price'
                    },
                    {
                        text: '台班单位',
                        width: 100,
                        sortable: false,
                        align: 'left',
                        dataIndex: 'MachineUnit'
                    },
                    {
                        text: '台班小计',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer( '0,000.000' ),
                        dataIndex: 'Amount'
                    },
                    {
                        text: '油料类型',
                        width: 80,
                        sortable: false,
                        align: 'left',
                        dataIndex: 'OilType'
                    },
                    {
                        text: '油料升数',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer( '0,000.000' ),
                        dataIndex: 'OilWear'
                    },
                    {
                        text: '油料单价',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer( '0,000.000' ),
                        dataIndex: 'OilPrice'
                    },
                    {
                        text: '油料小计',
                        width: 80,
                        sortable: false,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer( '0,000.000' ),
                        dataIndex: 'OilAmount'
                    },
                    {
                        text: '维修费用',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.numberRenderer('0,000.000'),
                        dataIndex: 'MaintenanceCost'
                    },
                    {
                        text: '养护日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'ApplicationDate'
                    },
                    {
                        text: '填报人',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Applicant'
                    },
                    {
                        text: '填报日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'CreateDate'
                    },
                    {
                        text: '审核人',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Auditor'
                    },
                    {
                        text: '审核日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'AuditedDate'
                    },
                    {
                        text: '批准人',
                        width: 120,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'Approver'
                    },
                    {
                        text: '批准日期',
                        width: 80,
                        sortable: true,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'ApprovedDate'
                    },
                    {
                        text: '延期填报',
                        width: 80,
                        sortable: true,
                        align: 'center',
                        renderer: renderGridReadonlyCheckBox,
                        dataIndex: 'IsDelay'
                    },
                    {
                        text: '备注',
                        xtype: 'templatecolumn',
                        tpl: '<span title="{Memo}">{Memo}</span> ',
                        width: 160,
                        minWidth: 160,
                        flex: 1,
                        sortable: false,
                        align: 'left',
                        dataIndex: 'Memo',
                        hideable: true
                    }
                ]
            }
        );

        $(window).bind
        (
            "resize",
            function ()
            {
                //调整表格宽度
                Common.AdjustGridWidth('maintainmachine-grid', 'body_content');
            }
        );

        //调整表格宽度
        Common.AdjustGridWidth('maintainmachine-grid', 'body_content');
    }
);
